<?php
/**
 * ClassName:
 * @auditor:leijunxiang
 * @time 2025/4/3 8:37
 */

namespace app\controller\freightForward;

use app\BaseController;
use app\factory\model\RequestAgreement;
use app\factory\model\ResponseAgreement;
use app\modelOperate\freightForward\ChannelDetailOpe;
use app\tool\freightForward\ChannelDetailHandle;
use app\tool\freightForward\HistoryPriceHandle;
use think\facade\Log;

class ChannelDetailController extends BaseController
{
    /**
     * 新增渠道详情
     * @return void
     */
    public function add()
    {
        $handle = app(ChannelDetailHandle::class);
        $response = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::class);
        $content = $requestAgreement->getContent();
        $areaName = isset($content['areaName']) ? $content['areaName'] : '';
        if (empty($areaName)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入区域名称！");
            return;
        }
        $weight = isset($content['weight']) ? $content['weight'] : '';
        if (empty($weight)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入公斤数！");
            return;
        }
        $price = isset($content['price']) ? $content['price'] : '';
        if (empty($price)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入价格！");
            return;
        }
        $channelId = isset($content['channelId']) ? $content['channelId'] : '';
        if (empty($channelId)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入所属渠道id！");
            return;
        }
        //新增详情(区域名称,公斤数,价格,所属渠道id)
        $flag = $handle->addDetail($areaName, $weight, $price, $channelId);
        if (!$flag) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
            $response->setContent($flag);
        }

    }

    /**
     * 查询渠道详情
     * @return void
     */
    public function select()
    {
        $handle = app(ChannelDetailHandle::class);
        $response = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::class);
        $content = $requestAgreement->getContent();
        $areaName = isset($content['areaName']) ? $content['areaName'] : '';
        $channelId = isset($content['channelId']) ? $content['channelId'] : '';
        if (empty($channelId)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入所属渠道id！");
            return;
        }
        $page = $content['page'] ? $content['page'] : 1;
        $limit = $content['limit'] ? $content['limit'] : 5;
        //查询详情查询详情(渠道id,页数，条数,区域名称)
        $detail = $handle->selectDetail($channelId, $page, $limit, $areaName);
        if (empty($detail)) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
            $response->setContent($detail);
        }
    }

    /**
     * 查询重量
     */
    public function selectWeight($channelId)
    {
        $channel = app(ChannelDetailHandle::class);
        $response = app(ResponseAgreement::class);
        $channelId = isset($channelId) ? $channelId : '';
        if (empty($channelId)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入所属渠道id！");
            return;
        }
        $weight = $channel->selectWeight($channelId);
        if (empty($weight)) {
            $response->setErrCode($channel->getErrCode());
            $response->setErrMsg($channel->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
            $response->setContent($weight);
        }
    }

    /**
     * 更新数据
     * @return void
     */
    public function update()
    {
        $handle = app(ChannelDetailHandle::class);
        $response = app(ResponseAgreement::class);
        $requestAgreement = app(RequestAgreement::class);
        $content = $requestAgreement->getContent();
        $id = isset($content['detailId']) ? $content['detailId'] : '';
        if (empty($id)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入详情id！");
            return;
        }
        $areaName = isset($content['areaName']) ? $content['areaName'] : '';
        if (empty($areaName)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入区域名称！");
            return;
        }
        $warehousePrice = isset($content['warehousePrice']) ? $content['warehousePrice'] : '';
        if (empty($warehousePrice)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入修改价格！");
            return;
        }
        $flag = $handle->updateChannelDetail($content);
        if (!$flag) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
        }
    }

    /**
     * 删除数据
     * @return void
     */
    public function delete()
    {
        $handle = app(ChannelDetailHandle::class);
        $response = app(ResponseAgreement::class);
        $requestObj = app(RequestAgreement::class);
        $attr = $requestObj->getContent()['detailIdAttr'];
        if (empty($attr)) {
            $response->setErrCode(FAIL);
            $response->setErrMsg("请输入详情id！");
            return;
        }
        $flag = $handle->delChannelDetail($attr);
        if (!$flag) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
        }
    }

    /**
     * 查询历史记录(详情id,开始时间,结束时间,页数,条数)
     * @return void
     */
    public function selectHistory($detailId, $startTime, $endTime, $page, $limit)
    {
        $handle = app(HistoryPriceHandle::class);
        $response = app(ResponseAgreement::class);
        //开始查询(详情id,开始时间,结束时间,页数,条数)
        $attr = $handle->startSelect($detailId, $startTime, $endTime, $page, $limit);
        if (!$attr) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setContent($attr);
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
        }
    }

    /**
     * 查询历史价格记录(详情id,开始时间,结束时间)
     * @param $detailId
     * @param $startTime
     * @param $endTime
     * @return void
     */
    public function selectHistoryPrice($detailId, $startTime, $endTime)
    {
        $handle = app(HistoryPriceHandle::class);
        $response = app(ResponseAgreement::class);
        //开始查询(详情id,开始时间,结束时间)
        $attr = $handle->startSelectPrice($detailId, $startTime, $endTime);
        if (!$attr) {
            $response->setErrCode($handle->getErrCode());
            $response->setErrMsg($handle->getErrMsg());
        } else {
            $response->setContent($attr);
            $response->setErrCode(SUCCESS);
            $response->setErrMsg("");
        }
    }
}